package com.fs.jzzn.service;

import java.util.ArrayList;
import java.util.List;

import lombok.AllArgsConstructor;
import lombok.Data;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.alibaba.fastjson.JSON;
import com.fs.base.vo.JsonResponse;
import com.fs.jzzn.pojo.Stock;
import com.fs.jzzn.pojo.StorageItem;
import com.fs.jzzn.pojo.TheLibrary;
import com.fs.jzzn.repository.ProjectRepository;
import com.fs.jzzn.repository.StockRepository;
import com.fs.jzzn.repository.StorageItemRepository;
import com.fs.jzzn.repository.TheLibraryRepository;

/**
 * @author TiddlerCJ
 * @desc 出库相关处理
 */
@AllArgsConstructor
@Data
@Service("theLibraryService")
public class TheLibraryService {
	private TheLibraryRepository theLibraryRepository;
	private StorageItemRepository storageItemRepository;
	private ProjectRepository projectRepository;
	private StockRepository stockRepository;
	
	@Transactional
	public void saveOrUpdata(TheLibrary theLibrary){
		/*
		 * 删除历史明细
		 */
		List<StorageItem> storageItems = storageItemRepository.findByBillNo(theLibrary.getBillNo())	;
		storageItemRepository.delete(storageItems);
		
		/*
		 * 保存最新数据
		 */
		List<StorageItem> newStorageItems = JSON.parseArray(theLibrary.getTheLibraryItemJson(),StorageItem.class);
		if(!CollectionUtils.isEmpty(newStorageItems)){
			for(StorageItem item: newStorageItems){
				item.setBillNo(theLibrary.getBillNo());
				item.setRoadNo(theLibrary.getRoadNo());
				item.setInOrOut("out");
			}
			storageItemRepository.save(newStorageItems);
		}
		theLibraryRepository.save(theLibrary);
	}
	
	@Transactional
	public void delete(String id){
		TheLibrary the = theLibraryRepository.findOne(id);
		List<StorageItem> storageItems = storageItemRepository.findByBillNo(the.getBillNo())	;
		theLibraryRepository.delete(id);
		storageItemRepository.delete(storageItems);
	}

	@Transactional
	public JsonResponse confirm(String id) {
		TheLibrary library = theLibraryRepository.findOne(id);
		List<StorageItem> list = storageItemRepository.findByBillNo(library.getBillNo());
		List<Stock> stocks = new ArrayList<>();
		for(StorageItem item: list){
			Stock stock = null;
			List<Stock>stockList = null;
			if(StringUtils.isNotBlank(item.getMType())){
				stockList = stockRepository.findByMNoAndMNameAndWarehouseNameAndLocationAndMTypeOrderByMCountAsc(item.getMNo(), item.getMName(), item.getWarehouseName(), item.getLocation(),item.getMType());
			}else{
				stockList = stockRepository.findByMNoAndMNameAndWarehouseNameAndLocationOrderByMCountAsc(item.getMNo(), item.getMName(), item.getWarehouseName(), item.getLocation());
			}
			if(CollectionUtils.isEmpty(stockList)){
				return JsonResponse.error(item.getMNo()+"/"+item.getMName()+"/"+item.getWarehouseName()+"/"+item.getLocation()+"记录无法索引！");
			}else{
				for (Stock node: stockList) {
					if(node.getMCount()>=item.getMCount()){
						stock = node; break;
					}
				}
				stock.setMCount(stock.getMCount() - item.getMCount());
			}
			stocks.add(stock);
		}
		/*
		 * 更新库存
		 */
		if(!CollectionUtils.isEmpty(stocks)){
			stockRepository.save(stocks);
		}
		/*
		 * 设置为已入库
		 */
		library.setStatus("1");
		theLibraryRepository.save(library);
		
		return JsonResponse.success();
	}
}
